﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using CNRS.CCP.Connector;

namespace CNRS.CCP.PasswordCrypt
{
	/// <summary>
	/// Console program to get en encryted string for password serialization in configuration file
	/// </summary>
	class Program
	{
		static void Main(string[] args)
		{
			Console.Write("Entrer le password : ");
			string encryptedData = SecurityHelper.EncryptString(getPassword());
			Console.Write(encryptedData);
			Console.WriteLine();
			Console.WriteLine("Copy and paste the above string in your configuration file.");
			Console.WriteLine("Press any key to close this programm !");
			Console.ReadKey();
		}

		private static SecureString getPassword()
		{
			SecureString pwd = new SecureString();
			while (true)
			{
				ConsoleKeyInfo i = Console.ReadKey(true);
				if (i.Key == ConsoleKey.Enter)
				{
					Console.WriteLine();
					break;
				}
				else if (i.Key == ConsoleKey.Backspace)
				{
					pwd.RemoveAt(pwd.Length - 1);
					Console.Write("\b \b");
				}
				else
				{
					pwd.AppendChar(i.KeyChar);
					Console.Write("*");
				}
			}
			return pwd;
		}

		private static SecureString getPassword(string password)
		{
			SecureString pwd = new SecureString();
			foreach (char c in password)
			{
				pwd.AppendChar(c);
			}

			return pwd;
		}
	}

}
